<?php

namespace app\common\model\order;

use app\common\model\user\Address;
use think\Model;
use traits\model\SoftDelete;

class Point extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'order_point';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名ntegral
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'paytime_text',
        'status_text',
        'image_str',
        'taketime_text'
    ];

    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : '';
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTaketimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['taketime']) ? $data['taketime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTaketimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function goods()
    {
        return $this->belongsTo('app\common\model\Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function address()
    {
        return $this->belongsTo('app\common\model\user\Address', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function yuOrder($goods, $num, $address_id, $uid)
    {
        $addressInfo = [];
        if (isset($address_id) && !empty($address_id)) {
            $addressInfo = Address::get($address_id);
            $goods['addressInfo'] = $addressInfo;
            $goods['address_id']  = $address_id;
        } else {
            $addressInfo = Address::getDefaultAddress($uid);
            if ($addressInfo) {
                $goods['addressInfo'] = $addressInfo;
                $goods['address_id']  = $addressInfo['id'];
            }
        }
        $goods['num'] = $num;
        $goods['total'] = bcmul($goods->point, $num, 0);
        $goods['express_charge'] = config('site.express_charge');
        return $goods;
    }
    public  function addData($data,  $uid)
    {
        $addressInfo = [];
        if ($data['addressInfo']) {
            $addressInfo = $data['addressInfo'];
        }
        $array = [
            'user_id' => $uid,
            'pay_point' =>  $data['total'],
            'express_charge' =>  $data['express_charge'],
            'pay_fee' =>  $data['pay_fee'],
            'pay_money' => bcadd($data['express_charge'], $data['pay_fee'], 2),
            'sn' => sn('order_point', 'sn', 'JF'),
            'address_id' => $data['address_id'],
            'goods_id' => $data['id'],
            'goods_name' => $data['title'],
            'image' => $data['image'],
            'integral' => $data['total'],
            'total' => $data['num'],
            'paytime' => time(),
            'consignee' => $addressInfo['contact'] ?? '',
            'phone' => $addressInfo['telephone'] ?? '',
            'address' => $addressInfo['province'] . '/' . $addressInfo['city'] . '/' . $addressInfo['district'] . '/' . $addressInfo['address'],
        ];
        $this->allowField(true)->save($array);
        return $this->id;
    }
}
